package com.beiming.libra.controller;

import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.UserProxyEnum;
import com.beiming.libra.domain.dto.requestdto.UserRequestDTO;
import com.beiming.libra.domain.dto.responsedto.UserResponseDTO;
import com.beiming.libra.helper.ContextUtil;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.UserRelationService;
import com.beiming.libra.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "个人中心基础信息管理", tags = "个人中心基础信息管理")
@RequestMapping("/api/userBasic")
public class UserBasicController {

  @Resource
  private UserService userService;

  @Resource
  private UserRelationService userRelationService;

  /**
   * 自然人基本信息查询
   */
  @GetMapping("/getUserBasic/{userId}")
  @ApiOperation(value = "自然人基本信息查询", notes = "自然人基本信息查询")
  public Result<UserResponseDTO> getUserBasic(
      @ApiParam(value = "用户id", required = true) @PathVariable("userId") String userId) {
    if (StringUtils.isEmpty(userId)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "用户id不能为空");
    }
    UserResponseDTO userMessage = userService.getUserById(userId);
    return Result.success(userMessage);
  }

  /**
   * 自然人基本信息编辑
   */
  @PostMapping("/updateUserBasic")
  @ApiOperation(value = "自然人基本信息编辑", notes = "自然人基本信息编辑")
  public Result updateUserBasic(UserRequestDTO userRequestDTO) {
    if (StringUtils.isEmpty(userRequestDTO.getUserId())) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "用户id不能为空");
    }
    userRelationService.updateUserBasic(userRequestDTO);
    return Result.success("操作成功");
  }

  /**
   * 更新用户手机号
   */
  @PostMapping("/updateMobilePhone/{mobilePhone}/{id}/{phoneCode}")
  @ApiOperation(value = "用户手机号码修改", notes = "用户手机号修改")
  public Result updateMobilePhone(
      @ApiParam(value = "手机号码", required = true) @PathVariable("mobilePhone") String mobilePhone,
      @ApiParam(value = "用户id", required = true) @PathVariable("id") String id,
      @ApiParam(value = "手机验证码", required = true) @PathVariable("phoneCode") String phoneCode) {
    if (StringUtils.isEmpty(mobilePhone) || StringUtils.isEmpty(id)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "id和手机号不能为空");
    }
    userRelationService.updateMobilePhone(mobilePhone, id, phoneCode);
    return Result.success();
  }

  /**
   * 更新用户邮件
   */
  @PostMapping("/updateEmail/{email}/{id}/{emailCode}")
  @ApiOperation(value = "用户邮箱修改", notes = "用户邮箱修改")
  public Result updateEmail(
      @ApiParam(value = "邮箱号", required = true) @PathVariable("email") String email,
      @ApiParam(value = "用户id", required = true) @PathVariable("id") String id,
      @ApiParam(value = "邮箱验证码", required = true) @PathVariable("emailCode") String emailCode) {
    if (StringUtils.isEmpty(email) || StringUtils.isEmpty(id)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "id和email不能为空");
    }
    userRelationService.updateEmail(email, id, emailCode);
    return Result.success();
  }

  /**
   * 手机号修改调用短信验证码
   */
  @PostMapping("/getPhoneCode/{mobilePhone}")
  @ApiOperation(value = "用户手机号修改获取验证码", notes = "用户手机号修改获取验证码")
  public Result getPhoneCode(
      @ApiParam(value = "手机号码", required = true) @PathVariable("mobilePhone") String mobilePhone) {
    if (StringUtils.isEmpty(mobilePhone)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "mobilePhone");
    }
    userRelationService.getPhoneCode(mobilePhone);
    return Result.success();
  }

  /**
   * 邮件送达修改获取验证码
   */
  @PostMapping("/getEmailCode/{email}")
  @ApiOperation(value = "用户邮箱修改获取验证码", notes = "用户邮箱修改获取验证码")
  public Result getEmailCode(
      @ApiParam(value = "用户邮箱", required = true) @PathVariable("email") String email) {
    if (StringUtils.isEmpty(email)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "email");
    }
    userRelationService.getEmailCode(email);
    return Result.success();
  }

  /**
   * 查询代理人身份关系类型数据
   */
  @GetMapping("/getProxyType")
  @ApiOperation(value = "查询代理人身份关系类型数据", notes = "0：律师,1：亲属/推荐公民/公司员工/其他")
  public Result getProxyType() {
    return Result.success(UserProxyEnum.getProxyType());
  }

  /**
   * 查询当事人类型数据
   */
  @GetMapping("/getPartType")
  @ApiOperation(value = "查询当事人类型数据", notes = "0：自然人,1：组织机构")
  public Result getPartType() {
    return Result.success(userRelationService.getPartyType());
  }

  /**
   * 个人中心个人实名认证
   */
  @PostMapping("/realNameVerify/{userName}/{code}")
  @ApiOperation(value = "个人中心个人实名认证", notes = "个人中心个人实名认证")
  public Result realNameVerify(
      @ApiParam(value = "姓名", required = true) @PathVariable("userName") String userName,
      @ApiParam(value = "身份证号", required = true) @PathVariable("code") String code) {
    if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(code)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "姓名和身份证号不能为空");
    }
    userRelationService.realNameVerify(ContextUtil.getCurrentUserId(), userName, code);
    return Result.success();
  }

}
